<?php
namespace app\api\model\food;

use app\common\model\food\Shop as ShopModel;
use app\api\model\Setting as SettingModel;
use hema\wechat\Pay as WxPay;
use think\facade\Cache;

class Shop extends ShopModel
{
    /**
     * 隐藏字段
     */
    protected $hidden = [];
    
    /**
     * 获取开通会员支付详情 - 微信回调
     */
    public function payDetail(string $order_no)
    {
        return Cache::get($order_no);
    }

    /**
     * 更新用户会员状态 - 微信回调
     */
    public function updatePayStatus(string $transaction_id, array $data)
    {
        //更新用户会员状态
		$user = User::get($data['user_id']);
		$user->score = ['inc', (int)$data['money']];//增加积分
		$user->pay = ['inc', $data['money']];//增加消费金额
		
		//增加消费记录
		$model = new Record;
		$arr = [];
        $arr[0] = $data;
		//开通会员才有积分
		if($user['v']['value'] > 0){
            $user->score = ['inc', $this->pay_price];//增加消费积分
            $arr[1] =$data;
		    $arr[1]['mode'] = 20;//消费
		    $arr[1]['type'] = 90;//积分
        }
        $model->saveAll($arr);
        $user->save();
        
        //账单分账
        if($data['type'] == 30){
            $wxpay = new WxPay(SettingModel::getItem('wxpay',$data['applet_id']));
            $wxpay->divide([
                'out_order_no' => $data['order_no'],
                'transaction_id' => $transaction_id,
                'total' => $data['money'],
            ],$data['applet_id']);//进行分账
        }
		return true;
    }
}
